package com.gzone.contentprovider.sql.dao;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;
import android.util.Log;

import com.gzone.contentprovider.helper.BitmapHelper;
import com.gzone.contentprovider.helper.DataHelper;
import com.gzone.contentprovider.sql.model.Category;
import com.gzone.contentprovider.sql.model.Item;
import com.gzone.contentprovider.utils.HTTPClientUtil;

public class CategoryDAO implements DAO<Category>{
	
	private static final String TABLE_NAME ="Category";
	private static final String ID ="Category_ID";
	private static final String TITLE ="Category_title";
	private static final String DESC ="Category_desc";
	private static final String APP_NAME ="Category_app_name";
	private static final String IMG ="Category_img";
	private static final String BG ="Category_bg";
	private static final String ORDER ="Category_order";
	private static final String SYNCDATE ="Category_syncdate";
	public  CategoryDAO() {
		DataHelper.dropTable(TABLE_NAME);
		String tableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
		+ ID + " INTEGER PRIMARY KEY," 
		+ TITLE + " TEXT," 
		+ DESC + " TEXT,"
		+ APP_NAME + " TEXT ,"
		+ IMG + " BLOB NULL,"
		+ BG + " BLOB NULL,"
		+ ORDER + " INTEGER,"
		+ SYNCDATE + " TEXT"
		+");";
		DataHelper.createTable(tableSql);
	}
	@Override
	public void delete(Category object) {
		// TODO Auto-generated method stub
		DataHelper.delete(TABLE_NAME, object.id);
	}

	@Override
	public ArrayList<Category> findAll() {
		// TODO Auto-generated method stub
		Cursor data = DataHelper.readAll(TABLE_NAME);
		ArrayList<Category> list = new ArrayList<Category>();
		Log.i("Category_DAO", "----> " + data.getCount());
		if(data.getCount() > 0){
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int app_name = data.getColumnIndex(APP_NAME);
			int img = data.getColumnIndex(IMG);
			int bg = data.getColumnIndex(BG);
			int order = data.getColumnIndex(ORDER);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do{
				Log.i("Category_DAO", "----> " + data.getCount());
				Category obj = new Category();
				obj.id = data.getInt(id);
				obj.title = data.getString(title);
				obj.desc = data.getString(desc);
				obj.app_name = data.getString(app_name);
				obj.img = data.getBlob(img);
				obj.bg = data.getBlob(bg);
				obj.order = data.getInt(order);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			}while(data.moveToNext());
		}
		data.close();
		return list;
	}

	@Override
	public Category findById(int id) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, ID, id);
		Category obj = null;
		if(data.getCount() > 0){
			data.moveToFirst();
			int cid = data.getColumnIndex(ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int app_name = data.getColumnIndex(APP_NAME);
			int img = data.getColumnIndex(IMG);
			int bg = data.getColumnIndex(BG);
			int order = data.getColumnIndex(ORDER);
			int syncdate = data.getColumnIndex(SYNCDATE);
				obj = new Category();
				obj.id = data.getInt(cid);
				obj.title = data.getString(title);
				obj.desc = data.getString(desc);
				obj.app_name = data.getString(app_name);
				obj.img = data.getBlob(img);
				obj.bg = data.getBlob(bg);
				obj.order = data.getInt(order);
				obj.syncdate = data.getString(syncdate);
		}
		data.close();
		return obj;
	}

	@Override
	public ArrayList<Category> findByParameter(String paraName, Object value) {
		// TODO Auto-generated method stub
		Cursor data = DataHelper.findByParam(TABLE_NAME, paraName, value);
		ArrayList<Category> list = new ArrayList<Category>();
		if(data.getCount() > 0){
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int app_name = data.getColumnIndex(APP_NAME);
			int img = data.getColumnIndex(IMG);
			int bg = data.getColumnIndex(BG);
			int order = data.getColumnIndex(ORDER);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do{
				Category obj = new Category();
				obj.id = data.getInt(id);
				obj.title = data.getString(title);
				obj.desc = data.getString(desc);
				obj.app_name = data.getString(app_name);
				obj.img = data.getBlob(img);
				obj.bg = data.getBlob(bg);
				obj.order = data.getInt(order);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			}while(data.moveToNext());
		}
		data.close();
		return list;
	}

	@Override
	public void insert(Category object) {
		object.img = BitmapHelper.getByteImageFromURL(object.imgURL);
		object.bg = BitmapHelper.getByteImageFromURL(object.bgURL);
		Object[] bindArgs = {object.id, object.title, object.desc,
						object.app_name, object.img,
						object.bg, object.order, object.syncdate};
		String query = "INSERT INTO " + TABLE_NAME + " ( "
								+ ID + "," 
								+ TITLE + "," 
								+ DESC + " ,"
								+ APP_NAME + ","
								+ IMG + ","
								+ BG + ","
								+ ORDER + ","
								+ SYNCDATE + ""
								+") VALUES (?,?,?,?,?,?,?,?);";
		DataHelper.insert(query , bindArgs);
	}

	@Override
	public void update(Category object) {
		// TODO Auto-generated method stub
		object.img = HTTPClientUtil.getImage(object.imgURL);
		object.bg = HTTPClientUtil.getImage(object.bgURL);
		Object[] bindArgs = {object.title, object.desc,
				object.app_name, object.img,
				object.bg, object.order, object.syncdate,object.id};
		String query = "UPDATE '" + TABLE_NAME + "' SET "
						+ TITLE + "=?," 
						+ DESC + "=?,"
						+ APP_NAME + "=?,"
						+ IMG + "=?,"
						+ BG + "=?,"
						+ ORDER + "=?,"
						+ SYNCDATE + "=? "
						+"where "+ ID +"=?;";
		DataHelper.update(query, bindArgs);
	}
	/**
	 * get data in field id and sysncdate 
	 */
	@Override
	public ArrayList<Category> findAllToUpdate() {
		// TODO Auto-generated method stub
		String query = "SELECT " + ID + ", " + SYNCDATE + " FROM '" + TABLE_NAME + "'"; 
		Cursor data = DataHelper.rawQuery(query, null);
		ArrayList<Category> list = new ArrayList<Category>();
		if(data.getCount() > 0){
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do{
				Category obj = new Category();
				obj.id = data.getInt(id);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			}while(data.moveToNext());
		}
		data.close();
		return list;
	}
	
	@Override
	public void deleteAllRow() {
		DataHelper.deleteAllRow(TABLE_NAME);
	}
	@Override
	public ArrayList<Category> readJSONData(String data) {
		ArrayList<Category> list = new ArrayList<Category>();
		try {
			JSONObject jSon = new JSONObject(data);
			System.out.println("---> json events: " + jSon);
			JSONArray arr = jSon.getJSONArray("category");
			JSONObject tmp;
			for(int i = 0; i < arr.length(); i++){
				tmp = arr.getJSONObject(i);
				Category obj = new Category();
				obj.id = tmp.getInt(ID);
				obj.title = tmp.getString(TITLE);
				obj.desc = tmp.getString(DESC);
				obj.app_name = tmp.getString(APP_NAME);
				obj.imgURL = tmp.getString(IMG);
				obj.bgURL = tmp.getString(BG);
				obj.order = tmp.getInt(ORDER);
				obj.syncdate = tmp.getString(SYNCDATE);
				list.add(obj);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
}
